package com.probiz.estore.system.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.probiz.estore.common.model.system.CompanyInfo;
import com.probiz.estore.common.model.system.SystemConfig;
import com.probiz.estore.core.event.ConfigChangedEvent;
import com.probiz.estore.core.event.EventUtil;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.system.dao.SystemConfigDao;
import com.probiz.estore.system.service.CompanyInfoManager;

/**
 * Implementation of SystemConfigManager interface. This basically transforms POJOs -> Forms and back again.
 * </p>
 * 
 * <p>
 * <a href="SystemConfigManagerImpl.java.html"> <i>View Source </i> </a>
 * </p>
 * 
 * @author <a href="mailto:r.so@sympatico.ca">Richard Che So </a>
 */
public class SystemConfigManagerImpl extends GenericManagerImpl<SystemConfig> implements
		com.probiz.estore.system.service.SystemConfigManager {

	private CompanyInfoManager cpMgr;

	private SystemConfigDao systemConfigDao = null;
	
	public void setCompanyInfoManager(CompanyInfoManager mgr) {
		this.cpMgr = mgr;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.system.service.SystemConfigManagerBase#saveSystemConfig(com.probiz.estore.common.model.system.SystemConfig)
	 */
	public void saveSystemConfig(SystemConfig systemConfig) {
		this.save(systemConfig);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.SystemConfigManager#getConfigMap()
	 */
	public Map<String,String> getConfigMap(){
		return getConfigMapNoCache();
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.SystemConfigManager#getConfigMapNoCache()
	 */
	public Map<String,String> getConfigMapNoCache(){
		List<SystemConfig> list = systemConfigDao.listAll();
		Map map = new HashMap<String,String>();
		for(SystemConfig systemConfig : list){
			map.put(systemConfig.getConfigKey(), systemConfig.getConfigValue());
		}
		return map;
	}

	public void saveConfigByKey(String configKey, String configVal) {
		if (configKey == null || "".equals(configKey))
			return;
		
		SystemConfig config = getConfigByKey(configKey);
		config.setConfigValue(configVal);
		saveSystemConfig(config);
	}

	public SystemConfig getConfigByKey(String configKey) {
		List<SystemConfig> configList = systemConfigDao.listByProperty("configKey", configKey);
		if (configList != null && configList.size() > 0) {
			return configList.get(0);
		}
		
		return null;
	}

	public CompanyInfo getCompanyInfo() {

		return this.cpMgr.getDefaultCompany();
	}
	

	@Override
	protected void initManager() {
		dao = systemConfigDao;
		
	}

	@Override
	protected void preDelete(SystemConfig entity) {
		
	}

	@Override
	protected void preSave(SystemConfig entity) {
		
	}

	public void setSystemConfigDao(SystemConfigDao systemConfigDao) {
		this.systemConfigDao = systemConfigDao;
	}
}
